package zhengqc.map;

import zhengqc.common.Pair;
import zhengqc.tree.AVLTree;

public class AVLTreeMap<K extends Comparable<K>, V> implements Map<K, V> {
    AVLTree<Pair<K, V>> avl;

    public AVLTreeMap() {
        this.avl = new AVLTree<>();
    }

    @Override
    public void put(K key, V value) {
        avl.add(new Pair<>(key, value));
    }

    @Override
    public V remove(K key) {
        return avl.remove(new Pair<>(key, null)).second;
    }

    @Override
    public V get(K key) {
        Pair<K, V> element = avl.get(new Pair<>(key, null));
        return element.second;
    }

    @Override
    public void set(K key, V value) {
        Pair<K, V> element = avl.get(new Pair<>(key, null));
        if (element == null) {
            throw new IllegalArgumentException("the key " + key + " is not exist!");
        }
        element.second = value;
    }
    @Override
    public boolean containsKey(K key) {
        return avl.get(new Pair<>(key, null)) != null;
    }
    @Override
    public int getSize() {
        return this.avl.getSize();
    }

    @Override
    public boolean isEmpty() {
        return this.avl.isEmpty();
    }

    public boolean isBalanced() {
        return avl.isBalanced();
    }
}
